<?php
/**
 * 快3中奖
 */
defined('IN_YuLin') || exit('NO PERMIT!');
echo '<pre>';
// 获取十一选五所有的彩种信息
$lotterySql  = 'SELECT lott.id, lott.`sname`, lott.`title`, lott.`ptid` FROM yulin_lottery AS lott
                LEFT JOIN yulin_lottery_category AS cate
                ON lott.`cate_id` = cate.`id`
                WHERE cate.`sname` = "k3"';
$lotteryList = $db->getall($lotterySql);

// 获取到所有的十一选五彩种，根据彩种设置sname信息来获取开奖接口内容
$qihaoTab    = Table('lottery_k3');
foreach ($lotteryList as $k => $v){
    // 获取当前彩种最新的开奖期号
    $qihaoSql  = 'SELECT id, period, tid, `code` FROM ' . $qihaoTab . ' WHERE tid = ' . $v['id'] . ' AND `code` != "" ORDER BY id DESC LIMIT 20';
    $qihaoList = $db->getall($qihaoSql);
    foreach ($qihaoList as $key => $value){
        // 获取当前期号后， 根据期号的id来获取所有当前彩种期号的投注项
        $allBetSql = 'SELECT item.id, item.o_id, item.period_id, item.lott_id, item.en_play, item.betting_no, item.betting_str, item.num, item.money, item.odds, item.winner, item.status,
                main.order_no,
                per.period
                FROM ' . Table('lottery_order_item') . ' as item
                LEFT JOIN ' . Table('lottery_order_main') . ' as main
                ON item.o_id = main.id
                    LEFT JOIN ' . Table('lottery_k3') . ' as per
                        ON item.period_id = per.id
                WHERE item.winner = "0" AND item.period_id = ' . $value['id'];
        $allBetList= $db->getall($allBetSql);
        if(!$allBetList || empty($allBetList)) continue;
        $value['code_arr'] = explode(',', $value['code']);
        foreach ($value['code_arr'] as $qk => $qv){
            $value['code_arr'][$qk] = intval($qv);
        }
        foreach ($allBetList as $ak => $av){
            checkZhongjiang($av, $value, $db);
        }
    }
}
echo 'END';

/**
 * 检查投注项是否中奖
 * @param $data : 投注内容
 * @param $kaijiang : 开奖结果
 * @param $db   : 数据库连接对象
 */
function checkZhongjiang($data, $kaijiang, $db){
    $update    = array(
        'winner' => 1, // 默认未中奖
    );
    $bet = $data['betting_no'];
    
    switch ($data['en_play']){
        case 'HE':
            $sum  = array_sum($kaijiang['code_arr']);
            $dx   = '小';
            $ds   = '双';
            if ($sum > 10) $dx = '大';
            if ($sum % 2) $ds  = '单';
            if ($bet == $dx || $bet == $ds || $bet == $sum) $update['winner'] = 2;
            break;
        case 'STHTX':
            $flag  = true;
            $first = array_shift($kaijiang['code_arr']);
            foreach ($kaijiang['code_arr'] as $k => $v){
                if ($v != $first){
                    $flag = false;
                    break;
                }else{
                    $flag = true;
                }
            }
            if ($flag) $update['winner'] = 2;
            break;
        case 'STHDX':
            if (implode('', $kaijiang['code_arr']) == $bet) $update['winner'] = 2;
            break;
        case 'SBTH':
            if (str_replace(',', '', $bet) == implode(',', $kaijiang['code_arr'])) $update['winner'] = 2;
            break;
        case 'SLHTX':
            $first = array_shift($kaijiang['code_arr']);
            if ($first == ($kaijiang['code_arr'][1] - 1) && $first == ($kaijiang['code_arr'][2] - 2)) $update['winner'] = 2;
            break;
        case 'ETHFX':
        case 'EBTH':
            $flag  = true;
            for($i = 0; $i < strlen($bet); $i++){
                if(!in_array(substr($bet, $i, 1), $kaijiang['code_arr'])) {
                    $flag = false;
                    break;
                }
            }
            if ($flag) $update['winner'] = 2;
            break;
        case 'ETHDX':
            if(str_replace(',', '', $bet) == implode('', $kaijiang['code_arr'])) $update['winner'] = 2;
            break;
    }
    $db->update('lottery_order_item', $update, 'id=' . $data['id']);
    $logFile   = '/www/wwwroot/one.qipai101.com/crontab/log/';
    $logStr    = date('Y-m-d H:i:s') . ': 中奖状态（' . '订单号：' . $data['order_no'] . '，投注号' . $data['betting_no'] . '， 期号' . $data['period'] . '）';
    openCodeLog($logFile, 'syxw_winner_log' . date('ymd') . '.txt', $logStr);
}


function openCodeLog($logDir, $fileName,  $str){
    // 检查路劲是否存在
    if (!file_exists($logDir)){
        mkdir($logDir, 0777, true);
    }
    $file = fopen($logDir . $fileName, "a+");
    fwrite($file, $str . "\r\n");
    fclose($file);
}